import { Entity, Column, JoinColumn, ManyToOne } from "typeorm";

import {
  Book,
  Voucher,
  BookMap,
  TonOrder,
  BIGINT_PG,
  INTEGER_PG,
  VARCHAR50_PG,
  VARCHAR255_PG,
  ENUM_BOOK_TYPE,
  BookMapGateway,
  BookContactMap,
  BookDefaultGateway,
  ENUM_BOOK_DIRECTION,
  BookContactMapGateway,
} from "qqlx-core";

import { TransformerBigInteger, TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar50, TransformerVarchar255 } from "@lib/orm.transfor.string";
import { TransformerEnum } from "@lib/orm.transfor.enum";

import { BigIdTradableSchema } from "@/alpha/ext.big-id.tradable";
import { IdTradableSchema } from "@/alpha/ext.id.tradable";

@Entity({ name: BookDefaultGateway.relations })
export class BookSchema extends BigIdTradableSchema implements Book {
  @Column({ transformer: new TransformerEnum(Object.values(ENUM_BOOK_TYPE), ENUM_BOOK_TYPE.NONE) })
  type: ENUM_BOOK_TYPE = ENUM_BOOK_TYPE.NONE;

  @Column({ transformer: new TransformerVarchar255() })
  remark: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerEnum(Object.values(ENUM_BOOK_DIRECTION), ENUM_BOOK_DIRECTION.NONE) })
  direction: ENUM_BOOK_DIRECTION = ENUM_BOOK_DIRECTION.NONE;

  @Column({ transformer: new TransformerVarchar50() })
  coupleId: VARCHAR50_PG = "";

  @Column({ transformer: new TransformerInteger(100) })
  amount: INTEGER_PG = 0;

  // ======

  @ManyToOne("TonOrderSchema", (s: TonOrder) => s.joinBookList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justTonOrder!: TonOrder;

  @ManyToOne("VoucherSchema", (s: Voucher) => s.joinBookList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justVoucher!: Voucher;
}

@Entity({ name: BookMapGateway.relations })
export class BookMapSchema extends BigIdTradableSchema implements BookMap {
  @Column({ transformer: new TransformerInteger() })
  type: ENUM_BOOK_TYPE = ENUM_BOOK_TYPE.NONE;

  @Column({ transformer: new TransformerVarchar255() })
  remark: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerBigInteger(100) })
  amount: BIGINT_PG = "0";

  @Column({ transformer: new TransformerBigInteger(100) })
  amountJie: BIGINT_PG = "0";

  @Column({ transformer: new TransformerBigInteger(100) })
  amountDai: BIGINT_PG = "0";

  @Column({ transformer: new TransformerVarchar255() })
  tradeId: VARCHAR255_PG = "";

  // ======

  @ManyToOne("TonOrderSchema", (s: TonOrder) => s.joinBookList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justTonOrder!: TonOrder;

  @ManyToOne("VoucherSchema", (s: Voucher) => s.joinBookList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justVoucher!: Voucher;
}

@Entity({ name: BookContactMapGateway.relations })
export class BookContactMapSchema extends IdTradableSchema implements BookContactMap {
  @Column({ transformer: new TransformerInteger() })
  type: ENUM_BOOK_TYPE = ENUM_BOOK_TYPE.NONE;

  @Column({ transformer: new TransformerVarchar255() })
  remark: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerBigInteger(100) })
  amount: BIGINT_PG = "0";

  @Column({ transformer: new TransformerBigInteger(100) })
  amountJie: BIGINT_PG = "0";

  @Column({ transformer: new TransformerBigInteger(100) })
  amountDai: BIGINT_PG = "0";

  @Column({ transformer: new TransformerVarchar255() })
  tradeId: VARCHAR255_PG = "";

  // ======

  @ManyToOne("TonOrderSchema", (s: TonOrder) => s.joinBookList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justTonOrder!: TonOrder;

  @ManyToOne("VoucherSchema", (s: Voucher) => s.joinBookList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justVoucher!: Voucher;
}
